require("../css/home.less")
require("../lib/fonts/iconfont.css")
//轮播图
require("../lib/swiper-bundle.min.css")
const Swiper=require("../lib/swiper-bundle.min.js")
document.ready(function(){
    // 渲染页脚
    window.$utils.addTabbar("home")

    //获取dom
    let rankNum=document.querySelector("#rankNum")      //今日排名
    let punchDay=document.querySelector("#punchDay")    //打卡天数
    let badgeNum=document.querySelector("#badgeNum")    //徽章
    let todayPunch=document.querySelector("#todayPunch")//今日打卡

    var mySwiper = new Swiper('.swiper-container', {
        direction: 'horizontal', // 垂直切换选项
        loop: true, // 循环模式选项
        autoHeight: true,
        autoplay: {
            delay: 3000,
            stopOnLastSlide: false,
            disableOnInteraction: false,
        },
        // 如果需要分页器
        pagination: {
            el: '.swiper-pagination',
        },
    })
    //获取本地存储的数据
      let userId= JSON.parse(window.localStorage.getItem("user")).userId
    //   console.log(userId.userId);
       
    //封装成一个函数方便调用
    getHomeData()
    function getHomeData(){
        //发送ajax
    axios.get(`http://139.9.177.51:8099/headPageInfo?userId=${userId}`)
    .then(function(res){
        let ret=res.data
        console.log(ret,"aaaa");
        if(ret.status==0){
            // 调用函数渲染数据
            render(ret.data)
        }
        
    })
    .catch(function(err){
        console.log(err);
    })
    }

    //渲染数据
    function render(obj){
        rankNum.innerHTML=obj.rank
        punchDay.innerHTML=obj.punchIn
        badgeNum.innerHTML=obj.insigniaNum

        //渲染打卡
        todayPunch.innerHTML=obj.isPunch==="false"? "今日打卡" : "已打卡"

        fale=obj.isPunch==="false"? false :true
        console.log(fale);
    }

    let fale=true

    //打卡事件
    todayPunch.addEventListener("click",function(){
        console.log(fale);
        if(fale){
            window.$utils.toast("!","今天已经打过卡了")
        }else{
        axios.get(`http://139.9.177.51:8099/clockIn?userId=${userId}`)
        .then(function(res){
            let ret=res.data
            if(ret.status==0){
                getHomeData()
                window.$utils.toast("√","打卡成功")
            }else{
                window.$utils.toast("×","打卡失败")
            }
        })
        .catch(function(err){
            console.log(err);
        })
        }
    })
})